<?php
class CategoryAction extends CommAction {
	function index(){
				
		import("ORG.Util.Page");
		$category = d('category');
		$count = $category->count();
		$p  = new Page ($count, 20 ); 
		
		$p->setConfig('header','篇记录');
        $p->setConfig('prev',"<");
        $p->setConfig('next','>');
        $p->setConfig('first','<<');
		$p->setConfig('last','>>');   
		
		$list = $category->limit($p->firstRow.','.$p->listRows)->findAll(); 
		//dump($list);

		$page = $p->show (); 

		$this->assign ( "page", $page );
        $this->assign ( "list", $list );
        $this->assign ("current_p",$p->nowPage);
		$this->display("Category:Category_list");
		
	}

	function category_add(){

		if($_POST['category_add']){
		
			if($_POST['cname']){

				$cname['cname'] = $_POST['cname'];
				$att_arr = $this->_upload("Category",false,360,240,true);
				

				$cname['pic'] = $att_arr['pic'];
				if(d("category")->add($cname)){

					//echo d("article")->getlastsql();
					$this->assign('jumpUrl',"__APP__/Category"); 
					//$this->success("添加产品分类成功!");
				}
				else{
					$this->assign('jumpUrl',"__APP__/Category"); 
					$this->success("添加产品分类失败!");
				}

			}
			else{
			
				$this->assign('jumpUrl',"__APP__/Category/category_add"); 
				$this->success("产品分类名不能为空!");
			}
			
		}
		else{			

	 		$this->display("Category:Category_add");
		}

	}

	function category_edit(){
		
		if($_POST['category_edit']){
		
			if($_POST['cname'] && $_POST['cid']){
				
				$category['cname'] = $_POST['cname'];
				$att_arr = $this->_upload("Category",false,360,240,true);

				if(!empty($att_arr)){
					$category['pic'] = $att_arr['pic'];
				}

				if(d("category")->where("cid = '".$_POST['cid']."'")->save($category)){

					//echo d("article")->getlastsql();
					$this->assign('jumpUrl',"__APP__/Category"); 
					$this->success("修改产品分类成功!");
				}
				else{
					$this->assign('jumpUrl',"__APP__/Category"); 
					$this->success("修改产品分类失败!");
				}
			}
			else{

				$this->assign('jumpUrl',"__APP__/Category"); 
				$this->success("产品分类名不能为空!");
			}			
			
		}
		else{			
			if($_GET['cid']){

				$c_array = d('category')->where("cid = '".$_GET['cid']."'")->find();
				
				//dump($c_array);

				$this->assign('c_array',$c_array); 
	 			$this->display("Category:Category_edit");
			}
			else{
				$this->assign('jumpUrl',"__APP__/Category"); 
				$this->success("产品分类id不能为空!");
			}
		}

	}

	function category_del(){
		
		$category_id = implode("','",$_POST['arr_row']);		
		//echo "id in ('".$category_id."')";
		if($category_id){
			if(d('category')->where("cid in ('".$category_id."')")->delete()){
				$msg = "删除分类成功!";
			}
			else{
				$msg = "删除分类失败!";
			}
		}
		else{
			$msg = "分类id不能为空";
		}
		//dump(d("category")->getlastsql());
		$this->assign('jumpUrl',"__APP__/Category"); 
		$this->success($msg);
	}






}

?>